package yf.mall.controller.api.dy;

import com.alibaba.fastjson.JSON;
import com.yf.annotation.IgnoreAuth;
import com.yf.response.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import yf.mall.common.Const;
import yf.mall.component.app.AppDyAuthCompoent;
import yf.mall.component.app.AppOrderComponent;
import yf.mall.dysdk.model.DyKolOrder;
import yf.mall.dysdk.model.DyToken;
import yf.mall.dysdk.model.hook.DarenOrderHook;
import yf.mall.entity.DyAccountAuthEntity;
import yf.mall.entity.UserEntity;
import yf.mall.enums.app.KolOrderFlowPointEnum;
import yf.mall.service.NoticeService;
import yf.mall.service.UserService;
import yf.mall.service.app.DyAccountAuthService;
import yf.mall.service.app.KolOrderService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


@Slf4j
@RestController
@RequestMapping("/api/dy/hook")
public class DyApiHookController {


    @Resource
    private DyAccountAuthService dyAccountAuthService;

    @Resource
    private NoticeService noticeService;

    @Resource
    private UserService userService;

    @IgnoreAuth
    @RequestMapping("/alliance_daren_order")
    public Map orderMessageNotify(@RequestBody String hook) {
        Map<String, Object> map = new HashMap<>();
        DarenOrderHook darenOrderHook = JSON.parseObject(hook, DarenOrderHook.class);
        DyKolOrder dyKolOrder = JSON.parseObject(darenOrderHook.getContent(), DyKolOrder.class);
        DyAccountAuthEntity authByOpenId = dyAccountAuthService.getAuthByOpenId(dyKolOrder.getAuthorOpenid());
        if (authByOpenId == null) {
            return map;
        }
        UserEntity userEntity = userService.get(authByOpenId.getUserId());
        noticeService.sendDyOrder(userEntity.getOpenId(), dyKolOrder.getAuthorAccount(), dyKolOrder.getOrderId().toString(), dyKolOrder.getTotalPayAmount().intValue(), dyKolOrder.getProductName(), dyKolOrder.getPaySuccessTime());
        kolOrderService.sendExpOrder(userEntity.getOpenId(), dyKolOrder);
        return map;
    }

    @Resource
    private KolOrderService kolOrderService;


}
